//
//  EarnTicketView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/10.
//

import UIKit

class EarnTicketView: UIView {
    
    lazy var ticketColView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 15
        layout.itemSize = CGSize(width: 54, height: 78)
        let col = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        col.showsVerticalScrollIndicator = false
        col.showsHorizontalScrollIndicator = false
        col.backgroundColor = .white
        col.delegate = self
        col.dataSource = self
        col.register(EarnTicketColCell.self, forCellWithReuseIdentifier: EarnTicketColCell.identifier)
        return col
    }()

    lazy var checkBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Check in", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.rx.tap.bind { [weak self] in
            self?.checkClosure?()
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(22)
        btn.isEnabled = false
        return btn
    }()
    
    var ticketArr = [TicketModel]() {
        didSet {
            ticketColView.reloadData()
        }
    }
    
    var checkClosure: ReturnClosure?

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(ticketColView)
        addSubview(checkBtn)
        ticketColView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(15)
            make.size.equalTo(CGSize.init(width: 261, height: 171))
        }
        checkBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-15)
            make.size.equalTo(CGSize.init(width: scaleWidth(219), height: 44))
        }
        wn_cornerWithBorder(8, 0.1, .white, 1, .init(white: 0, alpha: 0.1), .zero, 8, 1)
        layer.masksToBounds = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension EarnTicketView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        ticketArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EarnTicketColCell.identifier, for: indexPath) as! EarnTicketColCell
        cell.model = ticketArr[indexPath.item]
        return cell
    }
}
